package cn.icanci.ladybird.controller;

import cn.icanci.ladybird.common.result.JsonData;
import cn.icanci.ladybird.pojo.bo.AdminUpdateCarouselBO;
import cn.icanci.ladybird.pojo.bo.AdminUploadCarouselBO;
import cn.icanci.ladybird.pojo.entity.Carousel;
import cn.icanci.ladybird.service.CarouselService;
import cn.icanci.ladybird.service.admin.AdminUploadCarouselService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: icanci
 * @date: Created in 2020/10/7 0:12
 * @classAction: 处理轮播图的Controller
 */
@SuppressWarnings("all")
@Controller
@Slf4j
@RequestMapping("/api/v1/pri/admin")
public class AdminDataForCarouselController {
    @Autowired
    private CarouselService carouselService;
    @Autowired
    private AdminUploadCarouselService adminUploadCarouselService;
    @Autowired
    private AdminDataController adminDataController;

    /**
     * 跳转到 carousel-setting 轮播图设置
     *
     * @param model 数据处理对象
     * @return 返回视图地址
     */
    @RequestMapping("/carousel-setting")
    public String adminCarouselSetting(Model model, Integer pageNum, Integer pageSize) {
        PageInfo<Carousel> carouselPageInfo = carouselService.queryAll(pageNum, pageSize);
        JsonData jsonData = JsonData.builderSuccess(carouselPageInfo);
        model.addAttribute("jsonData", jsonData);
        return "admin/carousel-setting";
    }

    /**
     * 跳转到 carousel-setting-upload 上传 轮播图
     *
     * @param model 数据处理对象
     * @return 返回视图地址
     */
    @RequestMapping("/carousel-setting-upload")
    public String adminCarouselSettingUpload(Model model, HttpServletRequest request, AdminUploadCarouselBO adminUploadCarouselBO) {
        JsonData jsonData = adminUploadCarouselService.uploadCarousel(request, adminUploadCarouselBO);
        if (null == jsonData || jsonData.getCode().equals(-1)) {
            return adminDataController.admin500(model);
        } else {
            return adminCarouselSetting(model, null, null);
        }
    }

    @RequestMapping("/carousel-setting-update")
    public String adminCarouselSettingUpdate(Model model, Long carouselId) {
        Carousel carousel = carouselService.queryById(carouselId);
        model.addAttribute("jsonData", JsonData.builderSuccess(carousel));
        return "admin/carousel-setting-update";
    }


    @RequestMapping("/carousel-setting-update-go")
    public String adminCarouselSettingUpdateGo(Model model, AdminUpdateCarouselBO adminUpdateCarouselBO) {
        System.out.println(adminUpdateCarouselBO);
        Carousel carousel = new Carousel();
        carousel.setCarouselId(adminUpdateCarouselBO.getCarouselId());
        carousel.setCarouselImgLink(adminUpdateCarouselBO.getCarouselAddress());
        carousel.setCarouselTitle(adminUpdateCarouselBO.getCarouselName());
        carousel.setCarouselEnable(Integer.valueOf(adminUpdateCarouselBO.getCarouselStatus()));
        carousel.setCarouselWeight(Integer.valueOf(adminUpdateCarouselBO.getCarouselWeight()));
        carouselService.update(carousel);
        return adminCarouselSetting(model, null, null);
    }


    /**
     * 跳转到 carousel-setting-add 跳转到增加页面
     *
     * @return 返回视图地址
     */
    @RequestMapping("/carousel-setting-add")
    public String adminCarouselSettingAdd() {
        return "admin/carousel-setting-add";
    }
}
